

 class Stack {
    constructor(initSize){
        if(initSize<0){
            throw new Error('size 不能小于0');
        }
        this.arr = new Array(initSize||10);
        this.size = 0;  // 记录当前栈里元素的数量       
    }

    push(value){
       if(this.size===this.arr.length){
           throw new Error('栈已经满了');
       }
       this.arr[this.size++] = value;
    }

    pop(){
        if(this.size===0){
            throw new Error('栈已经是空的了！')
        }
        return this.arr[--this.size];
    }

    peek(){
        if(this.size===0){
            return null;
        }
        //return this.arr;
        return this.arr[this.size-1];// -1和-- 不一样
    }
    empty(){
        return this.size === 0;
    }
}


class Queue {
    constructor(initSize){
        this.arr =  new Array(initSize||10);
        this.size = 0;
        this.start = 0;
        this.end = 0;
    }

    push(value){
       if(this.size===this.arr.length){
           throw new Error('栈已经满了');
       }
       this.size++;
       this.arr[this.end] = value;
       this.end = this.end===this.arr.length-1?0:this.end+1;
    }
    poll(){
       if(this.size===0){
           throw new Error('队列已经是空的了！')
       }
       // 弹出最先进来的元素 并size--
       this.size--;
       let res = this.arr[this.start];
       this.start = this.start===this.arr.length-1?0:this.start+1;
       return res;
    }
    peek(){    
       if(this.size===0){
           return null;
       }
       return this.arr[this.start];
    }
    empty(){
        return this.size === 0;
    }
}
